package com.utils.picTxt
{
	import com.utils.picTxt.pic.FaceGather;
	import com.utils.picTxt.richTextField.RichTextField;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import flash.utils.setTimeout;
	
	/**输入框  */	
	public class InputTextPanel extends Sprite
	{
		private var _inputText:RichTextField;
		private var _faceGather:FaceGather; 
		private var _faceBtn:MovieClip;
		private var _sendBtn:MovieClip;
		private var _width:Number=0;
		private var _height:Number=0;
		private var _isSendTime:Boolean = true;
		private var  _onSendMessage:Function = new Function();
		public function InputTextPanel(faceBtn:MovieClip,sendBtn:MovieClip,width:Number=415,height:Number=105)
		{
			_faceBtn = faceBtn;
			_sendBtn = sendBtn;
			_width = width;
			_height = height;
			init();
		}
		
		private function init():void
		{
			addInputText();
			addBtn();
			addFacePanel();
		}
		
		/**添加表情容器*/		
		private function addFacePanel():void
		{
			if(!_faceGather) _faceGather = new FaceGather();
			_faceGather.returnTipTarget = returnTipTarget;
			_faceGather.x = _faceBtn.x+5;
			if(_faceGather.x + _faceGather.width > _width){
				_faceGather.x = _width - _faceGather.width-5;
			}
			_faceGather.y = _faceBtn.y - _faceGather.height;
			this.addChild(_faceGather);
			_faceGather.visible = false;
		}
		
		/**向输入框内添加表情*/		
		private function returnTipTarget(cls:Class):void
		{
			switchFacePanelVisiable();
			_inputText.insertSprite(new cls, _inputText.textfield.caretIndex);					
			if (_inputText.isSpriteAt(_inputText.caretIndex))
			{				
				_inputText.caretIndex++;
			}
		}
		
		/**添加按钮,设置坐标*/		
		private function addBtn():void
		{
			this.addChild(_faceBtn);
			_faceBtn.addEventListener(MouseEvent.CLICK,onClickHandler);
			
			this.addChild(_sendBtn);
			_sendBtn.x = this.width - _sendBtn.width;
			_sendBtn.y = _inputText.y + _inputText.height + _sendBtn.height;
			_sendBtn.addEventListener(MouseEvent.CLICK,onClickHandler);
			
			_inputText.y = _faceBtn.height;
		}
		
		private function onClickHandler(e:MouseEvent):void
		{
			switch(e.target){
				case _faceBtn:
					switchFacePanelVisiable(true);
					break;
				case _sendBtn:
					sendMessage(_inputText.exportXML());
					switchFacePanelVisiable();
					break;
			}
		}
		
		/**添加输入框 */		
		private function addInputText():void
		{
			_inputText = new RichTextField();
			_inputText.html = false;//关键
			_inputText.textfield.background = true;
			_inputText.textfield.backgroundColor = 0x111111;
			_inputText.setSize(_width, _height);
			_inputText.type = RichTextField.INPUT;
			_inputText.defaultTextFormat = new TextFormat("Courier New", 12, 0xFFFFFF, false, false, false);;
			_inputText.name = "input";
			this.addChild(_inputText);
		}
		
		/**发送信息*/		
		public function sendMessage(message:XML):void
		{
			if(!_inputText) return; 
			//发送内容为空
			if(!_isSendTime){
				trace('请等待3秒!');
				return;
			}
			setTimeout(setDealySendTime,3000);
			_isSendTime = false;
			_onSendMessage(MsgTranscoding.xmlToStr(message));
			_inputText.clear();
		}
		
		/**设置间隔发送时间*/		
		private function setDealySendTime():void{
			_isSendTime = true;
		}
		
		/**表情容器显隐*/		
		private function switchFacePanelVisiable(show:Boolean = false):void
		{
			_faceGather.visible = show;
		}
		
		
		public function set onSendMessage(value:Function):void{_onSendMessage = value;}
		
		override public function get height():Number{return _sendBtn.y + _sendBtn.height}
		
		
	}
}